﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


namespace FSC.HaNamAPApplication
{
    public partial class Doc : System.Web.UI.UserControl
    {
        public string CurrentUrlWithoutParam
        {
            get
            {
                return Controler.UrlWithoutParam(Request.RawUrl);
            }
        }

        //private bool IsSearchCurrent = false;

        protected int number = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData(false);
                BindDrop();
            }
        }

        /// <summary>
        /// Bind data into repeater
        /// </summary>
        /// <param name="isSearch">Is search ?</param>
        private void BindData(bool isSearch)
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);

            var data = from a in db.Documents
                       orderby a.Doc_ID descending
                       select new
                       {
                           ID = a.Doc_ID,
                           Name = a.Name,
                           Field = a.Field.Name,
                           Office = a.Office.Name,
                           FieldId = a.Field_ID,
                           OfficeId = a.Office_ID
                       };

            if (isSearch)
            {
                if (txtName.Text.Trim() != "")
                    data = from d in data where d.Name.Contains(txtName.Text.Trim()) select d;
                if (ddlField.SelectedValue != "-1")
                    data = from d in data where d.FieldId == int.Parse(ddlField.SelectedValue) select d;
                if (ddlOffice.SelectedValue != "-1")
                    data = from d in data where d.OfficeId == int.Parse(ddlOffice.SelectedValue) select d;
            }

            rptAP.DataSource = data;
            rptAP.DataBind();
        }

        private void BindDrop()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);

            // Bind Fields
            var dataField = from f in db.Fields
                            orderby f.Name
                            select f;
            ddlField.DataValueField = "Field_ID";
            ddlField.DataTextField = "Name";
            ddlField.DataSource = dataField;
            ddlField.DataBind();

            ddlField.Items.Insert(0, new ListItem("--Không lĩnh vực--", "-1"));

            // Bind Offices
            var dataOffice = from o in db.Offices
                             orderby o.Name
                             select new
                             {
                                 o.Office_ID,
                                 o.Name
                             };
            ddlOffice.DataValueField = "Office_ID";
            ddlOffice.DataTextField = "Name";
            ddlOffice.DataSource = dataOffice;
            ddlOffice.DataBind();

            ddlOffice.Items.Insert(0, new ListItem("--Không cơ quan--", "-1"));
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData(true);
        }
    }
}